package com.iemes.controller.client;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ClientFormMap;
import com.iemes.entity.RoleFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.entity.UserGroupsFormMap;
import com.iemes.mapper.UserGroupsMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.client.ClientMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;
import com.iemes.util.PasswordHelper;

@RequestMapping("/clientAccount/")
@Controller
public class ClientAccountController extends BaseController {
	
	@Inject
	private UserMapper userMapper;
	@Inject
	private ClientMapper clientMapper;
	@Inject
	private UserGroupsMapper userGroupsMapper;
	@RequestMapping("examine")
	public String getall(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/client/clientPerson_infomation_list";
	}
	
	@RequestMapping("clientperson_findByPage")
	@ResponseBody
	public PageView clientperson_findByPage(String pageNow, String pageSize) throws Exception {
		UserFormMap userFormMap=getFormMap(UserFormMap.class);
		List<Map<String, Object>> userFormMaps=userMapper.getAllclient(userFormMap);
		LuogicPaging(pageNow, pageSize, userFormMaps);
		return pageView;
	}
	@RequestMapping("clientAccount_edit")
	public String clientAccount_edit(Model model) {
		String id=getPara("id");
		UserFormMap userFormMap=userMapper.findbyFrist("id", id, UserFormMap.class);
		model.addAttribute("userEntity",userFormMap);
		return Common.BACKGROUND_PATH + "/client/clientPerson_infomation_edit";
	}
	@RequestMapping("clientperson_editInfo")
	@ResponseBody
	public String clientperson_editInfo(Model model) {
		UserFormMap userFormMap=getFormMap(UserFormMap.class);
		try {
			userMapper.editEntity(userFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	
	@RequestMapping("clientAccount_add")
	public String clientAccount_add(Model model) {
		ClientFormMap clientFormMap=getFormMap(ClientFormMap.class);
		List<ClientFormMap> clientFormMaps=clientMapper.findByWhere(clientFormMap);
		model.addAttribute("client",clientFormMaps);
		return Common.BACKGROUND_PATH + "/client/clientPerson_infomation_add";
	}
	
	@RequestMapping("clientperson_addInfo")
	@ResponseBody
	public String clientperson_addInfo(Model model) {
		UserFormMap userFormMap=getFormMap(UserFormMap.class);
		userFormMap.put("password", "123456");
		PasswordHelper passwordHelper = new PasswordHelper();
		passwordHelper.encryptPassword(userFormMap);
		userFormMap.put("createTime", DateUtils.getStringDateTime());
		userFormMap.put("locked", 1);
		userFormMap.put("deletestatus", 0);
		userFormMap.put("site", 0);
		try {
			userMapper.addEntity(userFormMap);
			UserGroupsFormMap userGroupsFormMap = getFormMap(UserGroupsFormMap.class);
			userGroupsFormMap.put("userid", userFormMap.get("id"));
			userGroupsFormMap.put("roleid", 5);
			userGroupsMapper.addEntity(userGroupsFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	
	// 删除客户操作
		@ResponseBody
		@RequestMapping("client_deleteEntity")
		@Transactional(readOnly = false)
		// 需要事务操作必须加入此注解
		@SystemLog(module = "客户账号管理", methods = "客户账号管理-删除账号")
		// 凡需要处理业务逻辑的.都需要记录操作日志
		public String deleteEntity() {
			String[] ids = getParaValues("ids");
			String Pid = null;
			for (String id : ids) {
				Pid = id;
				// orderMapper.deleteOrder(Integer.valueOf(id));
			}
			String[] value = Pid.split(",");
			for (String string : value) {
				userMapper.deleteUser(Integer.valueOf(string));
			}
			return "success";
		}
		
		// 密码重置
				@ResponseBody
				@RequestMapping("password_Rearh")
				@Transactional(readOnly = false)
				// 需要事务操作必须加入此注解
				@SystemLog(module = "客户账号管理", methods = "客户账号管理-重置账号")
				// 凡需要处理业务逻辑的.都需要记录操作日志
				public String password_Rearh() {
					String[] ids = getParaValues("ids");
					String Pid = null;
					for (String id : ids) {
						Pid = id;
						// orderMapper.deleteOrder(Integer.valueOf(id));
					}
					String[] value = Pid.split(",");
					
					UserFormMap userFormMap=getFormMap(UserFormMap.class);
					userFormMap.put("password", "123456");
					PasswordHelper passwordHelper = new PasswordHelper();
					passwordHelper.encryptPassword(userFormMap);
					for (String string : value) {
						userFormMap.put("id", string);
					}
					return "success";
				}

}
